package com.gebogebo.mobile.parking;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

public class ParkingActivity extends MapActivity implements OnClickListener {
    private static final String lg = "parkingMainActivity";
    private static final int INITIAL_ZOOM_LEVEL = 15;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        setMapParameters();
        setListeners();
    }

    private void setListeners() {
        findViewById(R.id.addButton).setOnClickListener(this);
        findViewById(R.id.findButton).setOnClickListener(this);
        findViewById(R.id.exitButton).setOnClickListener(this);
    }
    
    private void setMapParameters() {
        MapView mapView = (MapView) findViewById(R.id.mapView);
        mapView.getController().setZoom(INITIAL_ZOOM_LEVEL);
        
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if(location != null) {
            int lat = (int) (location.getLatitude() * 1E6);
            int lng = (int) (location.getLongitude() * 1E6);
            GeoPoint point = new GeoPoint(lat, lng);
            mapView.getController().setCenter(point);
        }

        mapView.displayZoomControls(false);
        mapView.setSatellite(false);
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    public void onClick(View v) {
        int viewId = v.getId();
        
        switch(viewId) {
        case R.id.exitButton:
            finish();
            break;
        case R.id.addButton:
            //go to add parking activity
            Intent intent = new Intent("com.gebogebo.mobile.parking.ACTION_ADD");
            startActivity(intent);
            break;
        case R.id.findButton:
            //start showing parking spots
            break;
         default:
            //unrecognized
            Log.i(lg, "click on unrecognized button");
        }
    }
}